package com.buanalintas.sandals.pos.payment;

import com.buanalintas.bynga.Action;
import com.buanalintas.bynga.Controller;
import com.buanalintas.bynga.Dictionary;
import com.buanalintas.bynga.Field;
import com.buanalintas.bynga.Page;
import com.buanalintas.bynga.Persistence;
import com.buanalintas.bynga.Utility;
import com.buanalintas.bynga.formatter.DateFormatter;
import com.buanalintas.bynga.formatter.MoneyFormatter;
import com.buanalintas.sandals.pos.entities.PosTransactionPayment;
import com.buanalintas.sandals.pos.entities.PosTransactionReceipt;
import java.math.BigDecimal;
import java.util.List;
import javax.persistence.EntityManager;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Payment extends Page {

    private Field paymentId;
    private Field paymentUserId;
    private Field paymentUnitId;
    private Field paymentDate;
    private Field paymentFirstGiroDate;
    private Field paymentFirstGiroNominal;
    private Field paymentSecondGiroDate;
    private Field paymentSecondGiroNominal;
    private Field paymentTriedGiroDate;
    private Field paymentTriedGiroNominal;
    private Field paymentFourGiroDate;
    private Field paymentFourGiroNominal;
    private Field paymentFiveGiroDate;
    private Field paymentFiveGiroNominal;
    private Field paymentReceiptId;
    private Field paymentTotal;
    private Field paymentKeel;
    private Field paymentCash;
    private Field paymentTimestamp;
    private Field paymentDescription;

    public Payment() {
        setTheClass(PosTransactionPayment.class);

        paymentId = new Field();
        paymentId.setName("paymentId");
        paymentId.setCaption("id");
        paymentId.setListProperties("");
        paymentId.setInputProperties("");
        getFields().add(paymentId);

        paymentUserId = new Field();
        paymentUserId.setName("paymentUserId");
        paymentUserId.setCaption("user");
        paymentUserId.setListProperties("");
        paymentUserId.setInputProperties(" size=\"20\" readonly");
        getFields().add(paymentUserId);

        paymentUnitId = new Field();
        paymentUnitId.setName("paymentUnitId");
        paymentUnitId.setCaption("unit");
        paymentUnitId.setListProperties("");
        paymentUnitId.setInputProperties(" size=\"20\" readonly");
        getFields().add(paymentUnitId);

        paymentReceiptId = new Field();
        paymentReceiptId.setName("paymentReceiptId");
        paymentReceiptId.setCaption("payment receipt");
        paymentReceiptId.setListProperties(" align=\"center\" width=\"10%\" nowrap");
        paymentReceiptId.setInputProperties("");
        paymentReceiptId.setRequired(true);
        paymentReceiptId.setSelectorCondition("t.receiptKeel='1'");
        paymentReceiptId.getSelectorAction().add(new String[]{"value", "paymentTotal", "receiptTotal"});
        paymentReceiptId.getSelectorAction().add(new String[]{"value", "paymentPaid", "receiptPaid"});
        paymentReceiptId.getSelectorColumns().add(new String[]{"receiptNumber", "Number", "width=\"10%\" nowrap"});
        paymentReceiptId.getSelectorColumns().add(new String[]{"receiptDate", " Date", "width=\"10%\" nowrap"});
        paymentReceiptId.getSelectorColumns().add(new String[]{"receiptTotal", "Total", " align=\"right\" width=\"10%\" nowrap"});
        paymentReceiptId.getSelectorColumns().add(new String[]{"receiptPaid", " Paid", " align=\"right\" width=\"10%\" nowrap"});
        paymentReceiptId.getSelectorColumns().add(new String[]{"receiptDescription", " Description", "width=\"70%\""});
        paymentReceiptId.setSelectorOrder("t.receiptNumber");
        getFields().add(paymentReceiptId);

        paymentDate = new Field();
        paymentDate.setName("paymentDate");
        paymentDate.setCaption("date");
        paymentDate.setListProperties(" align=\"center\" width=\"8%\" nowrap");
        paymentDate.setInputProperties(" size=\"8\" readonly");
        paymentDate.setRequired(true);
        paymentDate.setDefaultValue(Utility.getTimestamp());
        getFields().add(paymentDate);

        paymentFirstGiroDate = new Field();
        paymentFirstGiroDate.setName("paymentFirstGiroDate");
        paymentFirstGiroDate.setCaption("Giro-1");
        paymentFirstGiroDate.setListProperties(" align=\"center\" width=\"5%\" nowrap");
        paymentFirstGiroDate.setInputProperties(" size=\"8\" readonly");
        getFields().add(paymentFirstGiroDate);

        paymentFirstGiroNominal = new Field();
        paymentFirstGiroNominal.setName("paymentFirstGiroNominal");
        paymentFirstGiroNominal.setCaption("Giro-1 Nominal");
        paymentFirstGiroNominal.setListProperties(" align=\"right\" width=\"7%\" nowrap");
        paymentFirstGiroNominal.setInputProperties(" size=\"8\"");
        paymentFirstGiroNominal.setRequired(true);
        paymentFirstGiroNominal.setDefaultValue(BigDecimal.ZERO);
        paymentFirstGiroNominal.setFormater(MoneyFormatter.getInstance());
        getFields().add(paymentFirstGiroNominal);

        paymentSecondGiroDate = new Field();
        paymentSecondGiroDate.setName("paymentSecondGiroDate");
        paymentSecondGiroDate.setCaption("Giro-2");
        paymentSecondGiroDate.setListProperties(" align=\"center\" width=\"5%\" nowrap");
        paymentSecondGiroDate.setInputProperties(" size=\"8\" readonly");
        getFields().add(paymentSecondGiroDate);

        paymentSecondGiroNominal = new Field();
        paymentSecondGiroNominal.setName("paymentSecondGiroNominal");
        paymentSecondGiroNominal.setCaption("Giro-2 Nominal");
        paymentSecondGiroNominal.setListProperties(" align=\"right\" width=\"7%\" nowrap");
        paymentSecondGiroNominal.setInputProperties(" size=\"8\"");
        paymentSecondGiroNominal.setRequired(true);
        paymentSecondGiroNominal.setDefaultValue(BigDecimal.ZERO);
        paymentSecondGiroNominal.setFormater(MoneyFormatter.getInstance());
        getFields().add(paymentSecondGiroNominal);

        paymentTriedGiroDate = new Field();
        paymentTriedGiroDate.setName("paymentTriedGiroDate");
        paymentTriedGiroDate.setCaption("Giro-3");
        paymentTriedGiroDate.setListProperties(" align=\"center\" width=\"5%\" nowrap");
        paymentTriedGiroDate.setInputProperties(" size=\"8\"");
        getFields().add(paymentTriedGiroDate);

        paymentTriedGiroNominal = new Field();
        paymentTriedGiroNominal.setName("paymentTriedGiroNominal");
        paymentTriedGiroNominal.setCaption("Giro-3 Nominal");
        paymentTriedGiroNominal.setListProperties(" align=\"right\" width=\"7%\" nowrap");
        paymentTriedGiroNominal.setInputProperties(" size=\"8\"");
//        paymentTriedGiroNominal.setRequired(true);
        paymentTriedGiroNominal.setDefaultValue(BigDecimal.ZERO);
        paymentTriedGiroNominal.setFormater(MoneyFormatter.getInstance());
        getFields().add(paymentTriedGiroNominal);

        paymentFourGiroDate = new Field();
        paymentFourGiroDate.setName("paymentFourGiroDate");
        paymentFourGiroDate.setCaption("Giro-4");
        paymentFourGiroDate.setListProperties(" align=\"center\" width=\"5%\" nowrap");
        paymentFourGiroDate.setInputProperties(" size=\"8\"");
        getFields().add(paymentFourGiroDate);

        paymentFourGiroNominal = new Field();
        paymentFourGiroNominal.setName("paymentFourGiroNominal");
        paymentFourGiroNominal.setCaption("Giro-4 Nominal");
        paymentFourGiroNominal.setListProperties(" align=\"right\" width=\"7%\" nowrap");
        paymentFourGiroNominal.setInputProperties(" size=\"8\"");
//        paymentFourGiroNominal.setRequired(true);
        paymentFourGiroNominal.setDefaultValue(BigDecimal.ZERO);
        paymentFourGiroNominal.setFormater(MoneyFormatter.getInstance());
        getFields().add(paymentFourGiroNominal);

        paymentFiveGiroDate = new Field();
        paymentFiveGiroDate.setName("paymentFiveGiroDate");
        paymentFiveGiroDate.setCaption("Giro-5");
        paymentFiveGiroDate.setListProperties(" align=\"center\" width=\"5%\" nowrap");
        paymentFiveGiroDate.setInputProperties(" size=\"8\"");
        getFields().add(paymentFiveGiroDate);

        paymentFiveGiroNominal = new Field();
        paymentFiveGiroNominal.setName("paymentFiveGiroNominal");
        paymentFiveGiroNominal.setCaption("Giro-5 Nominal");
        paymentFiveGiroNominal.setListProperties(" align=\"right\" width=\"7%\" nowrap");
        paymentFiveGiroNominal.setInputProperties(" size=\"8\"");
//        paymentFiveGiroNominal.setRequired(true);
        paymentFiveGiroNominal.setDefaultValue(BigDecimal.ZERO);
        paymentFiveGiroNominal.setFormater(MoneyFormatter.getInstance());
        getFields().add(paymentFiveGiroNominal);

        paymentTotal = new Field();
        paymentTotal.setName("paymentTotal");
        paymentTotal.setCaption("total");
        paymentTotal.setListProperties(" align=\"right\" width=\"8%\" nowrap");
        paymentTotal.setInputProperties(" size=\"8\" readonly");
        paymentTotal.setRequired(true);
        paymentTotal.setFormater(MoneyFormatter.getInstance());
        getFields().add(paymentTotal);

        paymentCash = new Field();
        paymentCash.setName("paymentCash");
        paymentCash.setCaption("Cash");
        paymentCash.setListProperties(" align=\"right\" width=\"7%\" nowrap");
        paymentCash.setInputProperties(" size=\"8\"");
        paymentCash.setRequired(true);
        paymentCash.setFormater(MoneyFormatter.getInstance());
        getFields().add(paymentCash);

        paymentKeel = new Field();
        paymentKeel.setName("paymentKeel");
        paymentKeel.setCaption("keel");
        paymentKeel.setListProperties(" align=\"center\" width=\"5%\" nowrap");
        paymentKeel.setInputProperties(" size=\"8\"");
        paymentKeel.getOptions().add(new String[]{"1", "No"});
        paymentKeel.getOptions().add(new String[]{"2", "Yes"});
        getFields().add(paymentKeel);

        paymentTimestamp = new Field();
        paymentTimestamp.setName("paymentTimestamp");
        paymentTimestamp.setCaption("timestamp");
        paymentTimestamp.setListProperties(" align=\"center\" width=\"10%\" nowrap");
        paymentTimestamp.setInputProperties(" size=\"8\" readonly");
        paymentTimestamp.setDefaultValue(Utility.getTimestamp());
        getFields().add(paymentTimestamp);

        paymentDescription = new Field();
        paymentDescription.setName("paymentDescription");
        paymentDescription.setCaption("description");
        paymentDescription.setListProperties(" align=\"left\" width=\"40%\" nowrap");
        paymentDescription.setInputProperties(" cols=\"50\" rows=\"5\"");
        paymentDescription.setInputType(Field.INPUT_TEXT_AREA);
        paymentDescription.setInputCase(Field.CASE_UPPER);
        getFields().add(paymentDescription);

        fillFields();

        getFieldsAdd().clear();
        getFieldsAdd().add(paymentDate);
        getFieldsAdd().add(paymentReceiptId);
        getFieldsAdd().add(paymentTotal);
        getFieldsAdd().add(paymentCash);
        getFieldsAdd().add(paymentFirstGiroDate);
        getFieldsAdd().add(paymentFirstGiroNominal);
        getFieldsAdd().add(paymentSecondGiroDate);
        getFieldsAdd().add(paymentSecondGiroNominal);
        getFieldsAdd().add(paymentTriedGiroDate);
        getFieldsAdd().add(paymentTriedGiroNominal);
        getFieldsAdd().add(paymentFourGiroDate);
        getFieldsAdd().add(paymentFourGiroNominal);
        getFieldsAdd().add(paymentFiveGiroDate);
        getFieldsAdd().add(paymentFiveGiroNominal);
        getFieldsAdd().add(paymentDescription);

        getFieldsEdit().clear();
        getFieldsEdit().add(paymentDate);
        getFieldsEdit().add(paymentReceiptId);
        getFieldsEdit().add(paymentTotal);
        getFieldsEdit().add(paymentCash);
        getFieldsEdit().add(paymentFirstGiroDate);
        getFieldsEdit().add(paymentFirstGiroNominal);
        getFieldsEdit().add(paymentSecondGiroDate);
        getFieldsEdit().add(paymentSecondGiroNominal);
        getFieldsEdit().add(paymentTriedGiroDate);
        getFieldsEdit().add(paymentTriedGiroNominal);
        getFieldsEdit().add(paymentFourGiroDate);
        getFieldsEdit().add(paymentFourGiroNominal);
        getFieldsEdit().add(paymentFiveGiroDate);
        getFieldsEdit().add(paymentFiveGiroNominal);
        getFieldsEdit().add(paymentDescription);

        getFieldsList().clear();
        getFieldsList().add(paymentReceiptId);
        getFieldsList().add(paymentDate);
        getFieldsList().add(paymentTotal);
        getFieldsList().add(paymentFirstGiroDate);
        getFieldsList().add(paymentSecondGiroDate);
        getFieldsList().add(paymentTriedGiroDate);
        getFieldsList().add(paymentFourGiroDate);
        getFieldsList().add(paymentFiveGiroDate);
        getFieldsList().add(paymentDescription);

        getFieldsView().clear();
        getFieldsView().add(paymentReceiptId);
        getFieldsView().add(paymentUserId);
        getFieldsView().add(paymentUnitId);
        getFieldsView().add(paymentTotal);
        getFieldsView().add(paymentCash);
        getFieldsView().add(paymentFirstGiroDate);
        getFieldsView().add(paymentFirstGiroNominal);
        getFieldsView().add(paymentSecondGiroDate);
        getFieldsView().add(paymentSecondGiroNominal);
        getFieldsView().add(paymentKeel);
        getFieldsView().add(paymentTimestamp);
        getFieldsView().add(paymentDescription);

        getActions().remove(Action.EDIT);
        getActions().remove(Action.DELETE);
        getActions().remove(Action.DELETE_MULTI);

        setController(new PaymentController(this));
        getController().setOrder(paymentTimestamp);
        getController().setOrderType(Controller.ORDER_DESC);

        setExportList(getClass().getResourceAsStream("PaymentList.jasper"));
        setExport(getClass().getResourceAsStream("Payment.jasper"));
    }

//    @Override
//    public String validate(Object data) {
//        EntityManager em = getEntityManager();
//        PosTransactionPayment payment = (PosTransactionPayment) data;
//        //BigDecimal remaining = payment.getPaymentReceiptId().getReceiptTotal().subtract(payment.getPaymentReceiptId().getReceiptPaid());
//        BigDecimal total = payment.getPaymentCash().add(payment.getPaymentFirstGiroNominal()).add(payment.getPaymentSecondGiroNominal());
//            
//        if (total.compareTo(payment.getPaymentReceiptId().getReceiptTotal()) > 0) {
//            MoneyFormatter formatter = MoneyFormatter.getInstance();
//            return payment.getPaymentReceiptId() + " " + Dictionary.getValue(this, "Too much pay") + ", " + Dictionary.getValue(this, "total bill") + ": " + formatter.format(payment.getPaymentReceiptId().getReceiptTotal()) + " " + Dictionary.getValue(this, "money you pay now") + ": " + formatter.format(total);
//        }
//        return null;
//    }
    @Override
    public void afterInit(HttpServletRequest req, HttpServletResponse res) {
        paymentDate.setFormater(DateFormatter.getInstanceDate(this));
        paymentTimestamp.setFormater(DateFormatter.getInstanceDateTime(this));
    }

    class PaymentController extends Controller {

        public PaymentController(Page page) {
            super(page);
        }

        @Override
        public void beforeInsert(Object data) {
            PosTransactionPayment payment = (PosTransactionPayment) data;
            payment.setPaymentUnitId(getCurrentUnit());
            payment.setPaymentUserId(getCurrentUser());
            payment.setPaymentTimestamp(Utility.getTimestamp());
//            payment.setPaymentPaid(payment.getPaymentReceiptId().getReceiptPaid().add(payment.getPaymentPay()));
            payment.getPaymentReceiptId();
            BigDecimal total = payment.getPaymentCash().add(payment.getPaymentFirstGiroNominal()).add(payment.getPaymentSecondGiroNominal().add(payment.getPaymentTriedGiroNominal().add(payment.getPaymentFourGiroNominal().add(payment.getPaymentFiveGiroNominal()))));
//            BigDecimal total = payment.getPaymentCash();

            if (payment.getPaymentReceiptId().getReceiptTotal().compareTo(total) == 0) {
                payment.setPaymentKeel("2");
                payment.getPaymentReceiptId().setReceiptKeel("2");
                payment.getPaymentReceiptId().setReceiptPaid(payment.getPaymentReceiptId().getReceiptPaid().add(total));
                payment.getPaymentReceiptId().getReceiptPurchaseOrderId().setPurchaseOrderPaid(payment.getPaymentReceiptId().getReceiptPurchaseOrderId().getPurchaseOrderPaid().add(total));
                payment.getPaymentReceiptId().getReceiptPurchaseOrderId().setPurchaseOrderKeel("2");
            }else{
                payment.setPaymentKeel("1");
                payment.getPaymentReceiptId().setReceiptKeel("1");
                payment.getPaymentReceiptId().setReceiptPaid(payment.getPaymentReceiptId().getReceiptPaid().add(total));
                payment.getPaymentReceiptId().getReceiptPurchaseOrderId().setPurchaseOrderPaid(payment.getPaymentReceiptId().getReceiptPurchaseOrderId().getPurchaseOrderPaid().add(total));
                payment.getPaymentReceiptId().getReceiptPurchaseOrderId().setPurchaseOrderKeel("1");
            }
        }
    }

    @Override
    public void loadDetail(List datas, String id) {
        EntityManager em = getEntityManager();
        List<PosTransactionPayment> request = em.createQuery("SELECT t FROM PosTransactionReceipt t WHERE t.receiptDetailId.receiptId='" + id + "'").setHint(Persistence.REFRESH_HINT, true).getResultList();
        datas.clear();
        PosTransactionReceipt receipt = (PosTransactionReceipt) request;
        PosTransactionPayment payment = new PosTransactionPayment();
//        payment.setPaymentTotal(receipt.getReceiptTotal());
        payment.setPaymentDescription(receipt.getReceiptDescription());
        datas.add(payment);
    }

    @Override
    public void beforeExport() {
        getExportParams().put("title", Dictionary.getValue(this, "Payment"));
    }

    @Override
    public void beforeExportMulti() {
        beforeExport();
    }
}